package ciyuan;

import ciyuan.dataStore.Config;
import ciyuan.dataStore.PlayerIp;
import ciyuan.dataStore.PlayerLoginTime;
import ciyuan.wrap.Wrapper;

import java.io.File;
import java.net.ConnectException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 150149
 */
public class QQLogin {

    public static Config config;
    public static PlayerIp playerIp;
    public static PlayerLoginTime playerLoginTime;
    public static final String PREFIX = "§7[ §6QQ联合登录 §7] §f";
    private static final ExecutorService threads = Executors.newScheduledThreadPool(20);

    public static void onEnable() {
        Wrapper.INSTANCE.logMsg("-------[次元登录开启中]-------");
        Wrapper.INSTANCE.logMsg("作者: 150149");
        Wrapper.INSTANCE.logMsg("当前版本:" + Wrapper.INSTANCE.getPluginVersion());

        config = new Config(Wrapper.INSTANCE.loadConfig());
        playerIp = new PlayerIp(new File(Wrapper.INSTANCE.getDataFolder(), "Ip.yml"));
        playerLoginTime = new PlayerLoginTime(new File(Wrapper.INSTANCE.getDataFolder(), "LoginTime.yml"));

        Wrapper.INSTANCE.logMsg("-------[次元登录已开启]-------");
    }

    public static String onJoin(String playerName, String address) {
        if (playerLoginTime.isTimeout(playerName)) {
            return startCheckLogin(playerName, address);
        } else if (playerIp.isEqual(playerName, address)) {
            return allowLogin(playerName, address);
        } else {
            return startCheckLogin(playerName, address);
        }
    }

    private static String handleResponse(String response, String playerName, String address) {
        if (response.contains(".yz.")) {
            Matcher m = Pattern.compile("[^0-9]").matcher(response);
            return "登录失败，异地登录,请在Q群输入: 异地验证" + m.replaceAll("").trim();
        } else if (response.contains("1")) {
            return allowLogin(playerName, address);
        } else if (response.contains("0")) {
            return config.KickMessage;
        } else if (response.contains("-")) {
            return config.KickBlackMessage;
        } else {
            return "登录系统§c§n出错§f，请询问管理员";
        }
    }

    private static String startCheckLogin(String playerName, String address) {
        startCheckUpdate();
        try {
            Wrapper.INSTANCE.logMsg(PREFIX + "开始验证:" + playerName);
            String stringBuilder =
                config.CheckLoginURL + "/MClogin?id=" + URLEncoder.encode(playerName, "UTF-8") + "&server=" + config.ServerName + "&ip=" +
                    address;
            String response = Utils.getHttp(new URL(stringBuilder));
            return handleResponse(response, playerName, address);
        } catch (ConnectException e) {
            if (playerIp.isEqual(playerName, address)) {
                return allowLogin(playerName, address);
            } else {
                e.printStackTrace();
                return PREFIX + "登录系统§c§n连接失败";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return PREFIX + "登录系统§c§n连接失败";
        }
    }

    public static String allowLogin(String playerName, String address) {
        playerIp.setIp(playerName, address);
        playerLoginTime.setTime(playerName);
        return null;
    }

    public static void startCheckUpdate() {
        threads.execute(Updater::checkUpdate);
    }

}
